<#import "layout/layout.ftl" as u>
<@u.layout>
<div class="row tim-row">
    <h2 class="text-center">短链接[ ${shortUrlCode!} ]访问日志</h2>
    <legend></legend>
    <div class="col-md-10 col-md-offset-1">
        <table class="table table-hover" style="word-wrap: break-word; word-break: break-all;">
            <thead>
            <tr>
                <th>日志ID</th>
                <th>IP</th>
                <th>运营商</th>
                <th>区域</th>
                <th>国家</th>
                <th>省份</th>
                <th>城市</th>
                <th>访问时间</th>
                <th>链接状态</th>
                <th>文字描述</th>
            </tr>
            </thead>
            <tbody id="short-body">
            <tr>
                <th scope="row">-</th>
                <td>-</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
            </tr>
            </tbody>
        </table>
    </div>
    <div class="col-md-10 col-md-offset-1 text-center">
        <ul id='bp-element'></ul>
    </div>
</div>
<script src="/custom/js/bootstrap-paginator.min.js" type="text/javascript"></script>

<script>
    var SHORT_URL_CODE = "${shortUrlCode!}";

    $().ready(function () {
        var loginInfo = localStorage.getItem("loginInfo");
        if (loginInfo) {
            $("#username").text(JSON.parse(loginInfo).data.username);
            $("#no_login").hide();
            $("#logined").show();
        } else {
            $("#no_login").show();
            $("#logined").hide();
        }
        getList(1)
        $(window).on('scroll', materialKit.checkScrollForTransparentNavbar);
    });

    function getList(page) {
        var loginInfo = localStorage.getItem("loginInfo");
        var token = "";
        if (loginInfo) {
            token = JSON.parse(loginInfo).data.token
        }
        var settings = {
            type: "GET",
            url: "/api/log?page=" + page + "&code=" + SHORT_URL_CODE,
            success: function (data, textStatus) {
                if (data.code === 200) {
                    setData(data.data.records);
                    pager(data.data.current, data.data.pages);
                } else if (data.code === 1001) {
                    swal({
                        title: "需要登录！",
                        text: "请登录以获取更多信息！",
                        icon: "warning",
                        buttons: {
                            cancel: "回首页",
                            defeat: "去登录"
                        },
                    }).then((value) = > {
                        if(value === "defeat"
                )
                    {
                        window.location.href = '/user/login?redirectUrl=/short/' + SHORT_URL_CODE + '/log'
                    }
                else
                    {
                        window.location.href = '/'
                    }
                })
                    ;
                } else {
                    swal(data.message).then((value) = > {
                        window.location.href = '/'
                })
                    ;
                }
            },
            headers: {
                "Content-Type": "application/json",
                'Authorization': "Bearer " + token
            }
        };
        $.ajax(settings);
    }

    function setData(list) {
        console.log(list);
        var shortBody = $("#short-body");
        shortBody.html("");
        if (list.length === 0) {
            shortBody.html(
                    "<tr>\n" +
                    "    <th scope=\"row\">-</th>\n" +
                    "    <td>-</td>\n" +
                    "    <td>-</td>\n" +
                    "    <td>-</td>\n" +
                    "    <td>-</td>\n" +
                    "    <td>-</td>\n" +
                    "    <td>-</td>\n" +
                    "    <td>-</td>\n" +
                    "    <td>-</td>\n" +
                    "    <td>-</td>\n" +
                    "</tr>"
            );
        }
        for (var i in list) {
            var item = list[i];

            var reqIsp = item.reqIsp ? item.reqIsp : "-";
            var reqArea = item.reqArea ? item.reqArea : "-";
            var reqCountry = item.reqCountry ? item.reqCountry : "-";
            var reqRegion = item.reqRegion ? item.reqRegion : "-";
            var reqCity = item.reqCity ? item.reqCity : "-";

            var status = "短链接正常";
            switch (item.reqStatus) {
                case 1:
                    status = "过期无法访问";
                    break;
                case 2:
                    status = "被删除无法访问";
                    break;
                case 3:
                    status = "达到最大次数无法访问";
                    break;
            }

            var html =
                    "<tr>\n" +
                    "    <th scope=\"row\">" + item.id + "</th>\n" +
                    "    <td>" + item.reqIp + "</td>\n" +
                    "    <td>" + reqIsp + "</td>\n" +
                    "    <td>" + reqArea + "</td>\n" +
                    "    <td>" + reqCountry + "</td>\n" +
                    "    <td>" + reqRegion + "</td>\n" +
                    "    <td>" + reqCity + "</td>\n" +
                    "    <td>" + item.reqTime + "</td>\n" +
                    "    <td>" + status + "</td>\n" +
                    "    <td>" + item.reqMessage + "</td>\n" +
                    "</tr>";
            shortBody.append(html);
        }
    }

    var CURRENT_PAGE;

    function pager(currentPage, totalPages) {
        if (totalPages === 0) {
            return;
        }
        CURRENT_PAGE = currentPage;
        var element = $('#bp-element');
        var options = {
            bootstrapMajorVersion: 3, //对应的bootstrap版本
            currentPage: currentPage, //当前页数，从后台传过来的值
            numberOfPages: 5, //每页页数
            totalPages: totalPages, //总页数，从后台传过来的值
            itemTexts: function (type, page, current) {//设置显示的样式，默认是箭头
                switch (type) {
                    case "first":
                        return "首页";
                    case "prev":
                        return "上一页";
                    case "next":
                        return "下一页";
                    case "last":
                        return "末页";
                    case "page":
                        return page;
                }
            },
            //点击事件
            onPageClicked: function (event, originalEvent, type, page) {
                if (CURRENT_PAGE !== page) {
                    getList(page);
                }
            },
            itemContainerClass: function (type, page, current) {
                return (page === current) ? "active" : "pointer-cursor";
            }
        };
        element.bootstrapPaginator(options);
    }

    function cleanLS() {
        localStorage.removeItem("loginInfo");
        window.location.href = "/";
    }
</script>
</@u.layout>